package com.guli.edu.controller.admin;

import com.guli.common.vo.R;
import com.guli.edu.entity.Chapter;
import com.guli.edu.service.ChapterService;
import com.guli.edu.vo.ChapterVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author XiaoHua666
 * @date 2019/4/25
 */
@Api(tags = "课程章节管理")
@CrossOrigin
@RestController
@RequestMapping("/admin/edu/chapter")
public class ChapterAdminController {
    @Autowired
    private ChapterService chapterService;

    @ApiOperation(value = "新增章节")
    @PostMapping
    public R save(
            @ApiParam(value = "章节对象", name = "chapter", required = true)
            @RequestBody Chapter chapter
    ) {
        boolean result = chapterService.save(chapter);
        return result ? R.ok() : R.error().message("新增章节失败");
    }

    @ApiOperation(value = "根据ID删除章节")
    @DeleteMapping("{id}")
    public R removeById(
            @ApiParam(value = "章节id", name = "id", required = true)
            @PathVariable("id") String id
    ) {
        chapterService.removeChapterById(id);
        return R.ok();

    }

    @ApiOperation(value = "根据ID修改章节")
    @PutMapping("{id}")
    public R updateById(
            @ApiParam(value = "章节id", name = "id", required = true)
            @PathVariable("id") String id,
            @RequestBody Chapter chapter
    ) {
        chapter.setId(id);
        boolean result = chapterService.updateById(chapter);
        return result ? R.ok() : R.error().message("更新章节失败");
    }

    @ApiParam(value = "根据ID查询章节")
    @GetMapping("{id}")
    public R getById(
            @ApiParam(value = "查询章节", name = "id", required = true)
            @PathVariable("id") String id
    ) {
        Chapter chapter = chapterService.getById(id);
        return R.ok().data("item", chapter);
    }

    @ApiOperation(value = "嵌套章节数据列表")
    @GetMapping("/nested-list/{courseId}")
    public R nestedListByCourseId(
            @ApiParam(value = "课程id", name = "courseId", required = true)
            @PathVariable("courseId") String courseId
    ) {
        List<ChapterVo> chapterVoList = chapterService.nestedList(courseId);
        return R.ok().data("items", chapterVoList);
    }


}
